package com.gears_software.sxdc.dccx.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;


import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.finder.IZPOrderFinder;
import com.gears_software.sxdc.dccx.data.finder.util.GearsErrorHandler;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.data.model.ValuationRuleModel;
import com.gears_software.sxdc.dccx.ui.util.MaterialDialogProgress;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by SunMuran on 2017/9/8.
 * 计价规则的activity
 */
@EActivity(R.layout.activity_planning_rules)
public class PlanningRulesActivity extends Activity {
    @App
    DCCXApplication app;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView right_icon;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView tv_licheng;
    @ViewById
    TextView tv_shichang;
    @ViewById
    TextView tv_zuidi;
    @ViewById
    TextView tv_gaofeng_time;
    @ViewById
    TextView tv_gaofeng_xishu;
    @ViewById
    TextView tv_yejian_time;
    @ViewById
    TextView tv_yejian_xishu;
    @RestService
    IZPOrderFinder izpOrderFinder;
    @Bean
    GearsErrorHandler errorHandler;
    String facetype = "";
    ValuationRuleModel valuationRuleModel;
    MaterialDialogProgress progress;
    @AfterViews
    void created() {
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorActionbar));

        izpOrderFinder.setRestErrorHandler(errorHandler);
        progress = new MaterialDialogProgress(this);
        progress.show("正在加载...");
        initIconfont();
        Intent intent = getIntent();
        facetype = intent.getStringExtra("facetype");
        valuationRuleModel = new ValuationRuleModel();
        getFare();
    }

    void initIconfont() {
        back_icon.setTypeface(app.iconfont);
        back_icon.setText(R.string.back_icon);
        actionbar_title.setText("计价规则");
        right_icon.setVisibility(View.GONE);
    }

    @Background
    void getFare() {
        ResultModel<ValuationRuleModel> res = izpOrderFinder.getFare(facetype);
        showResult(res);
    }

    @UiThread
    void showResult(ResultModel<ValuationRuleModel> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                if (res.getData() != null) {
                    valuationRuleModel = res.getData();
                    tv_licheng.setText(valuationRuleModel.getUnitpricepermile() + "元/公里");
                    tv_shichang.setText(valuationRuleModel.getUnitpriceperminute() + "元/分钟");
                    tv_zuidi.setText(valuationRuleModel.getMinconsumption() + "元");
                    String morningpeaktimeon1 = valuationRuleModel.getMorningpeaktimeon().substring(0, 2);
                    String morningpeaktimeon2 = valuationRuleModel.getMorningpeaktimeon().substring(2, 4);
                    String morningpeaktimeoff1 = valuationRuleModel.getMorningpeaktimeoff().substring(0, 2);
                    String morningpeaktimeoff2 = valuationRuleModel.getMorningpeaktimeoff().substring(2, 4);
                    String eveningpeaktimeon1 = valuationRuleModel.getEveningpeaktimeon().substring(0, 2);
                    String eveningpeaktimeon2 = valuationRuleModel.getEveningpeaktimeon().substring(2, 4);
                    String eveningpeaktimeoff1 = valuationRuleModel.getEveningpeaktimeoff().substring(0, 2);
                    String eveningpeaktimeoff2 = valuationRuleModel.getEveningpeaktimeoff().substring(2, 4);
                    tv_gaofeng_time.setText("高峰(" + morningpeaktimeon1 + ":" + morningpeaktimeon2 + "-" + morningpeaktimeoff1
                            + ":" + morningpeaktimeoff2 + "," + eveningpeaktimeon1 + ":" + eveningpeaktimeon2 + "-" + eveningpeaktimeoff1 + ":"
                            + eveningpeaktimeoff2 + ")行驶时,加收高峰服务费");
                    tv_gaofeng_xishu.setText("高峰费=(里程费+时长费)*" + valuationRuleModel.getPeakunitprice());
                    String nighttimeon1 = valuationRuleModel.getNighttimeon().substring(0, 2);
                    String nighttimeon2 = valuationRuleModel.getNighttimeon().substring(2, 4);
                    String nighttimeoff1 = valuationRuleModel.getNighttimeoff().substring(0, 2);
                    String nighttimeoff2 = valuationRuleModel.getNighttimeoff().substring(2, 4);
                    tv_yejian_time.setText("夜间(" + nighttimeon1 + ":" + nighttimeon2 + "-" + "次日" + nighttimeoff1
                            + ":" + nighttimeoff2 + ")行驶时,加收夜间服务费");
                    tv_yejian_xishu.setText("夜间费=(里程费+时长费)*" + valuationRuleModel.getNightunitprice());
                }
            }
        }
        progress.dismiss();
    }

    @Click
    void back_icon() {
        finish();
    }
}
